<?php
/**
 * Created by PhpStorm.
 * User: liuchaozhong
 * Date: 2018/8/18
 * Time: 下午4:50
 */
namespace  App\Http\Controllers;

use App\Models\Checkin;
use App\Models\InviteLog;
use App\Models\Member;
use App\Models\ShareCheckIn;
use App\Models\ShareLog;
use App\Models\DemoPlay;
use App\Models\TaskConfigLog;
use App\Models\TaskConfig;
use App\Models\ViewLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;

class TaskController extends BaseController
{
    //签到任务
    public function checkIn()
    {
        $input = Input::all();
        $_member_info = $this->getMemberBy3rd($input['_3rd_session']);
        $input['member_id'] = $_member_info['id'];
        $model = new Checkin();
        $today = date('Y-m-d');
        $next_day = date('Y-m-d', strtotime("$today + 1 day"));
        $check = $model->isCheck($input['member_id'], $today);
        if($check){
            return $this->success('已经签到过~',['date'=> $today, 'flag'=>1, 'check_day'=>$_member_info['check_day']]);
        }
        if(isset($input['types']) && $input['types'] == 0){
            return $this->success('还没有签到',['date'=>$today, 'flag'=>0, 'check_day'=>$_member_info['check_day']]);
        }
        //判断用户是否连续签到
        $is_lx  = $model->isLx($input['member_id'], $today);
        if($is_lx) {
            $dd = $model->getCheckMoney($input['member_id']);
            $insert = [
                'member_id' => $input['member_id'],
                'money' => $dd['money'],
                'check_date' => $today,
                'next_date' => $next_day,
            ];
        }else {
            $dd = $model->getCheckMoney($input['member_id'], false);
            $insert = [
                'member_id' => $input['member_id'],
                'money' => $dd['money'],
                'check_date' => $today,
                'next_date' => $next_day,
            ];
        }
        //增加用户金币
        DB::beginTransaction();
        try {
            $member_info = Member::where('id', $input['member_id'])
                ->sharedLock()
                ->first();
            if($dd['day'] == ($dd['no']-1)) {
                $member_info->check_day = 1;
            }else {
                $member_info->check_day = $dd['day']+1;
            }
            $member_info->bi = $member_info->bi + $dd['money'];
            $member_info->save();
            //写入资金记录
            $capital_insert = [
                'type_id' => 1,
                'member_id' => $input['member_id'],
                'money' => $dd['money'],
                'bei' => sprintf('第%s天签到,奖励[%s]金币', $member_info->check_day, $dd['money']),
                'from_source' => getIp(),
                'date' => date('Y-m-d'),
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ];
            DB::table('capital_log')->insert($capital_insert);
            DB::table('checkin')->insert($insert);

            DB::commit();
        }catch (\Exception $e) {
            DB::rollBack();

            return $this->error('签到失败');
        }

        return $this->success('签到成功',[
            'date'=>$today,
            'flag'=>1,
            'check_day'=>$_member_info['check_day']
        ]);
    }

    //邀请
    public function inviteShare()
    {
        $input = Input::all();
        //获取openid
        $_3rd_session = Redis::get($input['_3rd_session']);
        $_3rd_session = unserialize($_3rd_session);
        //判断用户时候存在上级,如果存在则送金币,否者绑定关系
        if(!isset($input['pid'])){
            return $this->error('参数pid错误');
        }
        $member_info = Member::where('openid', $_3rd_session['openid'])->first();
        //判断自己是否是自己上级
        if($input['pid'] == $member_info->id) {
            return $this->error('不能自己是自己上级');
        }
        if(!empty($member_info) && $member_info->pid > 0) {
            $invites = InviteLog::where('invite_id',$input['pid'])
                ->where('binvite_id',$member_info->id)
                ->where('it_date',date('Y-m-d'))
                ->first();
            if(empty($invites)) {
                //todo 待定修改分享之后奖励
//                $invite_info = Member::where('id',$input['pid'])->first();
//                $invite_info->bi = $invite_info->bi + 10; //加10金币
//                $res = $invite_info->save();
                $res = true;
                if($res){
                    //添加邀请记录
                    //(new InviteLog())->addInviteLog($input['pid'], $member_info->id);
                    //添加任务记录
                    //(new TaskConfigLog())->addConfigLog($input['pid'], 2);
                    return $this->success('邀请成功');
                }else{
                    return $this->error('邀请失败');
                }
            }
            return $this->success('邀请成功');
        }else{
            $member_info->pid = $input['pid'];
            $res = $member_info->save();
            if($res){
                //添加邀请记录
                (new InviteLog())->addInviteLog($input['pid'], $member_info->id);
                //添加任务记录
                (new TaskConfigLog())->addConfigLog($input['pid'], 2);
                return $this->success('邀请成功');
            }else{
                return $this->error('邀请失败');
            }
        }

    }

    //观看视频
    public function viewVideo()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if(isset($input['article_id']) && $input['article_id'] > 0) {
            //获取所有观看纪录判断是否第一次观看
            $view_all = ViewLog::where('member_id', $member_info['id'])->count();
            //添加观看记录
            DB::beginTransaction();
            try{
                $res = (new ViewLog())->addView($member_info['id'], $input['article_id']);
                if($res == 'insert') {
                    if($view_all < 1) {
                        (new TaskConfigLog())->addConfigLog($member_info['id'], 5);
                    }
                    //添加任务记录
                    (new TaskConfigLog())->addConfigLog($member_info['id'], 6);
                }
                return $this->success('成功');
            }catch (\Exception $e){
                DB::rollBack();
                return $this->error('失败');
            }
            DB::commit();
        }else {
            return $this->error('不存在article_id');
        }
    }

    //分享视频
    public function shared()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if(isset($input['article_id']) && $input['article_id'] > 0) {
            //获取所有分享纪录判断是否第一次分享
            $share_all = ShareLog::where('member_id', $member_info['id'])
                ->where('types', 0)
                ->count();
            //添加分享记录
            DB::beginTransaction();
            try{
                $res = (new ShareLog())->addShare($member_info['id'], $input['article_id']);
                if($res == 'insert') {
                    if($share_all < 1) {
                        (new TaskConfigLog())->addConfigLog($member_info['id'], 10);
                    }
                    //添加任务记录
                    (new TaskConfigLog())->addConfigLog($member_info['id'], 7);
                }
                return $this->success('成功');
            }catch (\Exception $e) {
                DB::rollBack();
                return $this->error('失败');
            }
            DB::commit();
        }else {
            return $this->error('不存在article_id');
        }
    }

    //分享签到
    public function shareCheckIn()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if(!isset($input['content'])) {
            $input['content'] = '';
        }
        $res = (new ShareCheckIn())->addShareCheck($member_info['id'], $input['content']);
        if($res) {
            //添加任务记录
            (new TaskConfigLog())->addConfigLog($member_info['id'], 3);
        }
        if($res) {
            return $this->success('成功');
        }else {
            return $this->error('失败');
        }
    }

    //分享小程序
    public function sharedMini()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if(isset($input['types']) && $input['types'] == 1) {
            //获取所有分享纪录判断是否第一次分享
            $share_all = ShareLog::where('member_id', $member_info['id'])
                ->where('types', $input['types'])
                ->count();
            //添加观看记录
            $res = (new ShareLog())->addShare($member_info['id'], 0, $input['types']);
            if($res == 'insert') {
                if($share_all < 1) {
                    (new TaskConfigLog())->addConfigLog($member_info['id'], 8);
                }
                //添加任务记录
                (new TaskConfigLog())->addConfigLog($member_info['id'], 9);
            }
            if($res) {
                return $this->success('成功');
            }else {
                return $this->error('失败');
            }
        }else{
            return $this->error('参数types错误');
        }
    }

    //试玩金币任务
    public function demoPlay(){
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if($input['task_id']) {
            $task_id = $input['task_id'];
            $model = (new DemoPlay())->addTaskLog($task_id, $member_info['id']);
            if($model){
                return $model;
            }
        }else {
            return $this->error('不存在task_id');
        }
    }

    //分享好友观看
    public function sharedFriends()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        if(isset($input['types']) && $input['types'] == 2) {
            $res = (new ShareLog())->addShare($member_info['id'], 0, $input['types']);
            //添加任务记录
            (new TaskConfigLog())->addConfigLog($member_info['id'], 4);
            if($res) {
                return $this->success('成功');
            }else {
                return $this->error('失败');
            }
        }else{
            return $this->error('参数types错误');
        }
    }

    //视频弹框
    public function alertTaskConfig()
    {
        $input = Input::all();
        $member_info = $this->getMemberBy3rd($input['_3rd_session']);
        $finish_money = (new TaskConfigLog())->getalertConfig($member_info['id']);
        if(!empty($finish_money)) {
            return $this->success('成功', $finish_money);
        }else {
            return $this->error('失败');
        }
     }
}
